import { config } from '../../config';
import { Ability, Actor } from '@serenity-js/core';
import { connectToSalesforce, SalesforceAPI } from '../../salesforce';

export const useSystemAs = (rolename: string) => new UseSystem(rolename);

export class UseSystem implements Ability {
  constructor(private readonly rolename: string) {}

  public username(): string {
    return config.users[0].username;
  }

  public password() {
    return config.commonPassword;
  }

  public async salesforceApi(): Promise<SalesforceAPI> {
    const connection = connectToSalesforce({
      username: this.username(),
      password: this.password(),
      loginUrl: config.loginUrl,
    });
    return connection.api();
  }

  public static as(actor: Actor) {
    return actor.abilityTo(UseSystem);
  }
}
